﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RdH.XMLToolbox.Plugin;
using System.IO;
using System.Reflection;

namespace RdH.XMLToolbox
{
    static class PluginLoader
    {
        public static double MinimumHeight = 0;
        public static double MinimumWidth = 0;

        public static List<IPlugin> LoadPlugins(String path)
        {
            List<IPlugin> pluginList = new List<IPlugin>();

            string[] files = Directory.GetFiles(path, "*.dll");

            foreach (string file in files)
            {
                try
                {
                    Assembly assem = Assembly.LoadFrom(file);
                    System.Type[] types = assem.GetTypes();

                    foreach (System.Type type in types)
                    {
                        if (type.GetInterface("IPlugin") != null)
                        {
                            IPlugin plugin = (IPlugin)Activator.CreateInstance(type);
                            pluginList.Add(plugin);

                            if (plugin.MinimumHeight > MinimumHeight) { MinimumHeight = plugin.MinimumHeight; }
                            if (plugin.MinimumWidth > MinimumWidth) { MinimumWidth = plugin.MinimumWidth; }
                        }
                    }
                }
                catch (Exception ex)
                {
                    // LOG IT
                    throw ex;
                }

            }
            return pluginList;
        }
    }
}
